﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.PostManage
{
    public partial class PostAuditList : SecurityPage
    {
        public override string PrivCode()
        {
            string code = "";
            switch ((EnumData.ProjectType)int.Parse(Request.QueryString["TypeId"]))
            {
                case EnumData.ProjectType.公益活动:
                    code = "7df78fc381821845";
                    break;
                case EnumData.ProjectType.勤工助学:
                    code = "3b152a22f5e18930";
                    break;
                case EnumData.ProjectType.社会实践:
                    code = "137fec4a3a1c5dfe";
                    break;
                case EnumData.ProjectType.志愿活动:
                    code = "e18c45158691c53b";
                    break;
            }
            return code;
        }

        public string GetPrivCode(EnumData.PrivBtnType EnumType)
        {
            return UtilityUI.GetPrivCodeByBtn(Request.QueryString["TypeId"].ToString(), EnumType);
        }

        int i = 0;
        public string GetXN()
        {
            if (i == 0)
                i = (this.AspNetPager1.PageSize) * (this.AspNetPager1.CurrentPageIndex - 1);

            i += 1;
            return i.ToString();
        }

        public string GetXYName(string XYDM)
        {
            xydm md = xydm.Where("XYDM=@0", XYDM).FirstOrDefault();
            return md == null ? "" : md.XYMC;
        }

        public string GetTypeName()
        {
            return Enum.GetName(typeof(EnumData.ProjectType), Convert.ToInt32(Request.QueryString["TypeId"]));
        }

        private void DropPositionBing()
        {
            List<TypeInfo> typelist = TypeInfo.Where("ProjectTypeid=@0", Convert.ToInt32(Request.QueryString["TypeId"])).ToList();
            typelist.Add(new TypeInfo() { id = -1, TypeName = "全部" });
            typelist = typelist.OrderBy(p => p.id).ToList();
            this.Drop_ProjectType.DataSource = typelist;
            this.Drop_ProjectType.DataValueField = "id";
            this.Drop_ProjectType.DataTextField = "TypeName";
            this.Drop_ProjectType.DataBind();

            this.Drop1.Items.Clear();
            List<xy_bmxbm> xy_bmxbm_list = xy_bmxbm.Where("BMFQID=@0", "03").ToList();
            this.Drop1.Items.Add(new ListItem() { Value = "0", Text = "全部" });
            xy_bmxbm_list.ForEach(p => {
                this.Drop1.Items.Add(new ListItem() { Value = p.BMDM, Text = p.BMMC});
                this.AddChildNodes(p.BMDM);
            });
        }

        private void AddChildNodes(string BMDM)
        {
            List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0", BMDM).ToList();
            list.ForEach(p =>
            {
                this.Drop1.Items.Add(new ListItem() { Value = p.BMDM, Text ="├─"+ p.BMMC });
            });
        }


        public string GetPositonName(string postionid)
        {
            return TypeInfo.Where("id=@0", int.Parse(postionid)).First().TypeName;
        }

        public string GetDepartmentName(string Departmentid)
        {
            xy_bmxbm md = xy_bmxbm.Where("BMDM=@0", Convert.ToString(Departmentid)).FirstOrDefault();
            return md == null ? "" : md.BMMC;
        }

        private void RepPostBing()
        {
            UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
            string xymc = "";
            if (Convert.ToBoolean(Umd.IsStudent))
                xymc = xy_xsxx.Where("XH=@0", Umd.XH).First().XY;
            else
                xymc = xy_jgxx.Where("ZGH=@0", Umd.XH).First().XYMC;

            xydm xymd = xydm.Where("XYMC=@0", xymc).FirstOrDefault();

            var eps = DynamicLinqExpressions.True<Project>();
            eps = eps.And(p => p.IsDel == false && p.IsAudit == true && p.ProjectTypeid == Convert.ToInt32(Request.QueryString["TypeId"]));
            eps = eps.And(p => p.Title.Contains(this.txt_title.Text.Trim()));

            switch ((EnumData.DataRole)Convert.ToInt32(Umd.DataRole))
            {
                case EnumData.DataRole.学校管理员:

                    break;
                case EnumData.DataRole.学院管理员:
                    eps = eps.And(p => p.College == xymd.XYDM);
                    break;
                case EnumData.DataRole.部门管理员:
                    if (Convert.ToBoolean(Umd.IsStudent))
                    {
                        eps = eps.And(p => p.College == "-1");//学生没有隶属部门
                    }
                    else
                    {
                        xy_jgxx md = xy_jgxx.Where("ZGH=@0", Umd.XH).First();
                        eps = eps.And(p => p.Departmentid == md.BMDM);
                    }
                    break;
                case EnumData.DataRole.学生管理员:
                    eps = eps.And(p => p.CreateUserid == Umd.id);
                    break;
            }
            
            if (this.Drop_ProjectType.SelectedValue != "-1")
            {
                int Typeid = Convert.ToInt32(this.Drop_ProjectType.SelectedValue);
                eps = eps.And(p => p.Typeid == Typeid);
            }

            if (this.Drop1.SelectedValue != "0")
            {
                string SearchDepttext = this.Drop1.SelectedItem.Text;
                if (SearchDepttext.IndexOf("├─") > -1)
                {
                    string Departmentid = this.Drop1.SelectedValue;
                    eps = eps.And(p => p.Departmentid == Departmentid);
                }
                else
                {
                    string SearchBMDM = this.Drop1.SelectedValue;
                    List<xy_bmxbm> list = xy_bmxbm.Where("BMFQID=@0", SearchBMDM).ToList();
                    var epsD = DynamicLinqExpressions.True<Project>();
                    list.ForEach(p =>
                    {
                        epsD = epsD.Or(o => o.Departmentid == p.BMDM);
                    });
                    eps = eps.And(epsD);
                }
            }

            int RowCount = LinqUtility.LinqContext.Project.Where(eps).Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<Project> newslist = LinqUtility.LinqContext.Project.Where(eps).OrderByDescending(p => p.AddTime).Skip(SkipCount).Take(PageSize).ToList();
            this.RepPostList.DataSource = newslist;
            this.RepPostList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DropPositionBing();
            RepPostBing();
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
        }

        protected void RepPostList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int Projectid = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "Del")
            {
                Project md = Project.Where("id=@0", Projectid).First();
                md.IsDel = true;
                this.RepPostBing();
            }
            else if (e.CommandName == "UnAudit")
            {
                if (Apply.Where("Projectid=@0 and IsDel=@1", Convert.ToInt32(Projectid), false).Count() > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {alert('存在已申请的人员，不能取消审批！'); });</script>");
                    return;   
                }
                UserList Umd = UserList.Where("UserName=@0", User.Identity.Name).First();
                Project md = Project.Where("id=@0", Projectid).First();
                md.IsAudit = false;
                md.Auditid = Umd.id;
                md.AddTime = DateTime.Now;
                Project.Update(md, "id=@0", Projectid);
                this.RepPostBing();
            }
            else
            {
                Project md = Project.Where("id=@0", Projectid).First();
                this.lab_Departmen.Text = xy_bmxbm.Where("BMDM=@0", md.Departmentid).First().BMMC;
                this.lab_Addtime.Text = md.AddTime.ToString();
                this.lab_Contents.Text = md.Contents;
                this.lab_CreateUser.Text = UserList.Where("id=@0", md.CreateUserid).First().UName;
                this.lab_FuZheRenName.Text = md.FuZheRenName;
                this.lab_PositionType.Text = this.GetPositonName(md.PositionTypeId.ToString());
                this.lab_RecruitCount.Text = md.RecruitCount.ToString();
                this.lab_Tel.Text = md.Tel;
                this.lab_title.Text = md.Title;
                this.HidProjectId.Value = Projectid.ToString();
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () {Showdlg() });</script>");
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepPostBing();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepPostBing();
        }

        public int GetUnAuditPersonal(int Projectid, bool IsAudit)
        {
            return Apply.Where("Projectid=@0 and IsAudit=@1 and IsDel=@2", Convert.ToInt32(Projectid), IsAudit,false).Count();
        }
    }
}